<?php

namespace app\controller;

use app\BaseController;
use app\service\PartnerUserService;
use think\exception\ValidateException;
use think\facade\Request;

/**
 * 合作商用户
 */
class Partneruser extends BaseController
{
    //合作商用户列表
    public function user_lists()
    {
        $uid = Request::param('uid/d', 0);
        $param['partner_id'] = get_partner_id($uid);
        $param['page'] = Request::param('page/d', 0);
        $param['limit'] = Request::param('limit/d', 10);
        $data = PartnerUserService::getAllPartnerUser($param);
        returnSuccess(200, '获取成功', $data);
    }

    //添加合作商用户
    public function add_user()
    {
        $uid = Request::param('uid/d', 0);
        $param['uid'] = $uid;
        $param['partner_id'] = get_partner_id($uid);
        $param['name'] = Request::param('name/s', '');
        $param['mobile'] = Request::param('mobile/s', '');
        $param['nickname'] = Request::param('nickname/s', '');
        $param['password'] = Request::param('password/s', '');
        $param['status'] = Request::param('status/d', 1);
        $param['role_id'] = Request::param('role_id/d', 0);
        $param['ip'] = Request::ip();
        try {
            validate(\app\validate\PartnerUser::class)->check([
                'mobile' => $param['mobile'],
                'password' => $param['password'],
                'nickname' => $param['nickname'],
                'role_id' => $param['role_id'],
                'name' => $param['name']
            ]);
        } catch (ValidateException $e) {
            returnFail(21009, $e->getError());
        }
        $resCode = PartnerUserService::add_user($param);
        if ($resCode == 10000) {
            returnSuccess(200, '添加成功');
        } elseif ($resCode == 10001) {
            returnFail(204, '此手机号已存在');
        } else {
            returnFail(205, '添加失败');
        }
    }

    //修改合作商用户
    public function modify_user()
    {
        $uid = Request::param('uid/d', 0);
        $param['uid'] = $uid;
        $param['partner_id'] = get_partner_id($uid);
        $param['name'] = Request::param('name/s', '');
        //用户id
        $id = Request::param('id/d', 0);
        $param['mobile'] = Request::param('mobile/s', '');
        $param['nickname'] = Request::param('nickname/s', '');
        $param['password'] = Request::param('password/s', '');
        $param['status'] = Request::param('status/d', 1);
        $param['role_id'] = Request::param('role_id/d', 0);
        $param['ip'] = Request::ip();
        try {
            validate(\app\validate\PartnerUser::class)->check([
                'mobile' => $param['mobile'],
                'password' => $param['password'],
                'nickname' => $param['nickname'],
                'role_id' => $param['role_id'],
                'name' => $param['name']
            ]);
        } catch (ValidateException $e) {
            returnFail(21009, $e->getError());
        }
        $resCode = PartnerUserService::modify_user($param, $id);
        if ($resCode == 10000) {
            returnSuccess(200, '修改成功');
        } elseif ($resCode == 10001) {
            returnFail(204, '此手机号已存在');
        } else {
            returnFail(205, '修改失败');
        }
    }

    //删除合作商用户
    public function del_user()
    {
        $uid = Request::param('uid/d', 0);
        $param['uid'] = $uid;
        $param['partner_id'] = get_partner_id($uid);
        $id = Request::param('id/d', 0);
        $param['ip'] = Request::ip();
        $resCode = PartnerUserService::del_user($param, $id);
        if ($resCode == 10000) {
            returnSuccess(200, '删除成功');
        } elseif ($resCode == 10001) {
            returnFail(204, '不存在此用户');
        } elseif ($resCode == 10002) {
            returnFail(204, '删除权限不正确');
        } else {
            returnFail(205, '删除失败');
        }
    }

    public function uinfo()
    {
        $uid = Request::param('uid/d', 0);
        $param['partner_id'] = get_partner_id($uid);
        $data = PartnerUserService::getAllPartnerInfoById($uid);
        returnSuccess(200, '获取成功', $data);
    }

    public function uinfowms()
    {
        $uid = Request::param('uid/d', 0);
        $param['partner_id'] = get_partner_id($uid);
        $data = PartnerUserService::getAllPartnerInfoById($uid);
        returnSuccess(200, '获取成功', $data);
    }

    public function platforminfo()
    {
        $uid = Request::param('uid/d', 0);
        $param['partner_id'] = get_partner_id($uid);
        $data = PartnerUserService::platforminfo($param['partner_id']);
        returnSuccess(200, '获取成功', $data);
    }
}
